package com.llq.studentinfo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.llq.studentinfo.Session;
import com.llq.studentinfo.Student;
import com.llq.util.DateUtil;

abstract public class SessionTest {

	private Session session;
	private static final String department = "ENGL";
	private static final String number = "103";
	// private static final String name1 = "lee";
	// private static final String name2 = "lee2";
	private static final int CREDITS = 3;
	private Date startDate;

	@Before
	public void creatSession() {
		startDate = DateUtil.creatDate(2009, 1, 6);
		session = creatSession(creatCourse(), startDate);
		session.setNumberOfCredits(CREDITS);
	}

	abstract public Session creatSession(Course course,
			Date startDate);

	@Test
	abstract public void countTest();

	@Test
	abstract public void sessionDateTest();

	@Test
	public void creatTest() {

		assertEquals(department, session.getCourse().getDepartment());
		assertEquals(number, session.getCourse().getNumber());
		assertEquals(0, session.getNumberOfStudents());

	}

	@Test
	public void enrollStudentTest() {

		Student student1 = new Student("sA");
		session.enroll(student1);
		assertEquals(1, session.getNumberOfStudents());
		assertEquals(student1, session.getStudent(1));
		assertEquals(CREDITS, student1.getCredits());

		Student student2 = new Student("sB");
		session.enroll(student2);
		assertEquals(2, session.getNumberOfStudents());
		assertEquals(student1, session.getStudent(1));
		assertEquals(CREDITS, student1.getCredits());
		assertEquals(student2, session.getStudent(2));
		assertEquals(CREDITS, student2.getCredits());

	}

	@Test
	public void iterateTest() {
		Session session = creatSession(creatCourse(), startDate);
		session.enroll(new Student("sA"));
		session.enroll(new Student("sB"));
		session.enroll(new Student("sC"));
		List<Student> list = new ArrayList<Student>();
		for (Student student : session) {
			list.add(student);
		}
		assertEquals(list, session.getAllStudents());
	}

	private Course creatCourse() {
		// TODO Auto-generated method stub
		return new Course(department, number);
	}

	@Test
	public void comparableTest() {
		Session session1 = creatSession(new Course("bcd", "103"), startDate);
		Session session2 = creatSession(new Course("Cde", "103"), startDate);
		Session session3 = creatSession(new Course("bcd", "103"), startDate);
		Session session4 = creatSession(new Course("bcd", "098"), startDate);

		assertTrue(session1.compareTo(session2) < 0);
		assertTrue(session2.compareTo(session1) > 0);
		assertEquals(0, session1.compareTo(session3));
		assertTrue(session1.compareTo(session4) > 0);
		assertTrue(session4.compareTo(session1) < 0);
	}

	@Test
	public void sessionLength() {
		Session session = creatSession(new Course("",""), new Date());
		assertTrue(session.getSessionLength() > 0);
	}
//	@Test
//	public void sessionUrlTest(){
//		final String url="http://www.google.com/doc";
//		if (session.setUrl(url)) {
//			assertEquals(url, session.getUrl().toString());
//		}
//	}
	@Test
	public void urlTest() throws SessionException {
		final String url="http://www.google.com/doc";
		session.setUrl(url);
		assertEquals(url, session.getUrl().toString());
		
		final String url2="https://www.google.com/doc";
		session.setUrl(url2);
		assertEquals(url2, session.getUrl().toString());
	}
	
	@Test
	public void invalidUrlTest(){
		final String url="htstp://www.google.com/doc";
		try {
			session.setUrl(url);
			fail();
		} catch (SessionException e) {
			Throwable cause=e.getCause();
			assertEquals(MalformedURLException.class, cause.getClass());
		}
	}


}
